<script setup>
import { ref, onMounted } from 'vue';
import { pointsApi } from '@/utils/api';

const userId = ref(1); // 从登录后的存储中获取
const totalPoints = ref(0);
const thresholds = ref({
  reward: 100,
  penalty: 20
});
const rewards = ref([]);
const penalties = ref([]);
const pointsHistory = ref([]);
const isLoading = ref(false);

// 加载积分数据
const loadPointsData = async () => {
  isLoading.value = true;
  try {
    // 获取积分基本信息
    const pointsInfo = await pointsApi.getPoints(userId.value);
    totalPoints.value = pointsInfo.totalPoints;
    thresholds.value = {
      reward: pointsInfo.rewardThreshold,
      penalty: pointsInfo.penaltyThreshold
    };
    
    // 获取奖励列表
    const rewardsData = await pointsApi.getRewards(userId.value);
    rewards.value = rewardsData || [];
    
    // 获取惩罚列表
    const penaltiesData = await pointsApi.getPenalties(userId.value);
    penalties.value = penaltiesData || [];
    
    // 获取积分历史
    const historyData = await pointsApi.getPointsHistory(userId.value);
    pointsHistory.value = historyData || [];
  } catch (error) {
    console.error('加载积分数据失败:', error);
  } finally {
    isLoading.value = false;
  }
};

// 兑换奖励
const redeemReward = async (rewardId) => {
  try {
    if (confirm('确定要兑换这个奖励吗？')) {
      await pointsApi.redeemReward(userId.value, rewardId);
      loadPointsData(); // 重新加载数据
    }
  } catch (error) {
    console.error('兑换奖励失败:', error);
  }
};

// 接受惩罚
const acceptPenalty = async (penaltyId) => {
  try {
    if (confirm('确定要接受这个惩罚吗？')) {
      await pointsApi.acceptPenalty(userId.value, penaltyId);
      loadPointsData(); // 重新加载数据
    }
  } catch (error) {
    console.error('接受惩罚失败:', error);
  }
};

// 更新积分设置
const updateSettings = async (settings) => {
  try {
    await pointsApi.updatePointsSettings(userId.value, settings);
    loadPointsData();
  } catch (error) {
    console.error('更新设置失败:', error);
  }
};

onMounted(() => {
  loadPointsData();
});

// 其余表单处理函数...
</script>

<!-- 模板部分保持不变 --> 